<template>
  <div class="app-container">
    <el-form inline>
      <el-form-item>
        <el-button type="primary" @click="(outerVisible = true), (form.title = ''), (form.content = ''), (mode = 'add')">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="content">
      <div class="_list">
        <el-table :data="dataList" highlight-current-row v-loading.body="listLoading" :height="$tools.tableHeight()" :default-sort="{prop: 'date', order: 'descending'}" @selection-change="handleSelectionChange" style="width: 100%">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column label="标题" prop="title">
          </el-table-column>
          <!-- <el-table-column label="帮助详情" prop="content">
        </el-table-column> -->
          <el-table-column label="发布时间" width="260" sortable>
            <template slot-scope="scope">
              {{ scope.row.modifyTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="280">
            <template slot-scope="scope">
              <el-button type="text" @click="Toview(scope.row.content)">查看</el-button>
              <el-button type="text" @click="modMessage(scope.row)">修改</el-button>
              <el-button type="text" @click="arrDelete('1', scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pages">
        <div class="left">
          <el-tooltip class="item" effect="dark" content="批量删除" placement="top">
            <el-button size="small" type="danger" @click="arrDelete('2')">批量删除</el-button>
          </el-tooltip>
        </div>
        <div class="right" v-if="total > 0">
          <el-pagination background :total="total" :page-size="15" :current-page.sync="page" layout="total, prev, pager, next, jumper" @current-change="handleCurrentChange">
          </el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="添加信息" width="1060px" :visible.sync="outerVisible">
      <el-form :model="form" ref="form" label-width="60px" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input placeholder="请输入标题" v-model.trim="form.title" maxlength="100"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <ueditor v-model="form.content" :config="config"></ueditor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false">取 消</el-button>
        <el-button type="primary" v-loading="loading" @click="submit">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看信息" width="1030px" :visible.sync="isToview">
      <div class="dialog-content" v-html="dialogContent"></div>
    </el-dialog>
  </div>
</template>

<script>
import mixinsTable from '@/util/mixins.table';
import ueditor from '@/components/Editor';

export default {
  name: 'helpList',
  mixins: [mixinsTable],
  components: {
    ueditor
  },
  data () {
    return {
      id: '',
      loading: false,
      mode: 'add',
      isToview: false,
      dialogContent: null,
      form: {
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 10 到 52 个字符', trigger: 'blur' }
        ],
        content: [{ required: true, message: '请输入商品详情', trigger: 'change' }]
      },
      config: {
        initialFrameWidth: 958,
        initialFrameHeight: 420
      },
      outerVisible: false,
      APISERVER: 'getHelpList',
      DELETEAPI: 'deleteHelp'
    };
  },
  created () {
    this.getList();
  },
  methods: {
    submit () {
      if (this.loading) return;
      this.loading = true;

      this.$refs.form.validate(async valid => {
        if (valid) {
          let result = '';

          if (this.mode === 'add') {
            result = await this.$API.addHelp(this.form);
          } else {
            result = await this.$API.modHelp(this.id, this.form);
          }
          const { code } = result;

          if (code === 200) {
            this.loading = false;
            this.outerVisible = false;
            this.$message.success(`${this.mode === 'add' ? '添加' : '编辑'}成功!`);
            this.getList();
          }
        }
      });
    },
    Toview (data) {
      this.dialogContent = data;
      this.isToview = true;
    },
    modMessage (row) {
      this.id = row.id;
      this.mode = 'mod';
      this.form.title = row.title;
      this.outerVisible = true;
      setTimeout(() => {
        this.form.content = row.content;
      }, 200);
    }
  }
};
</script>

<style lang="less" scoped>
.el-popover {
  padding: 6px;
}
.editArea {
  padding: 3px;
  cursor: pointer;
  &:hover {
    margin: -1px;
    border: 1px dashed #666;
  }
}
.con {
  line-height: 35px;
  padding: 10px;
}
.dialog-content {
  line-height: 30px;
}
.content {
  span.reference {
    display: block;
    height: 40px;
    line-height: 40px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
}
.thumbnail {
  width: 120px;
  .img {
    width: 100%;
    height: auto;
  }
}
.product_info {
  padding: 3px 0;
  overflow: hidden;
  .thumbnail {
    width: 50px;
    height: 50px;
    float: left;
    img {
      width: 50px;
      height: 50px;
    }
  }
  .info {
    overflow: hidden;
    padding-left: 10px;
    .price {
      color: red;
      font-size: 14px;
    }
    .p_price {
      color: #b7b5b5;
      text-decoration: line-through;
    }
    ._name {
      display: block;
      font-size: 13px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
